﻿<?php

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

$lockfile = DISCUZ_ROOT.'./data/misc_cron.lock';
@$filemtime = filemtime($lockfile);
if(TIMESTAMP - $filemtime < 3) exit();//5秒传一次
touch($lockfile);


@set_time_limit(0);

Cron_forum_attach();

function Cron_forum_attach(){
	global $_G;
	if($_G['setting']['ftp']['on'] !='2') return;
	require_once libfile('function/forum');
	$sql = '';
	if($minsize = intval($_G['setting']['ftp']['minsize'])){
		$minsize = $minsize * 1024;
		$sql .= ' AND filesize >'.$minsize;
	}
	$attach_forum = $remoteaids = array();
	$attach_forum_tableid = mt_rand(0,9);
	$BIGfile_size = 10;//单位MB 大文件标识 如果有超过*MB的文件 本次只更新一个大文件
	$query = DB::fetch_all("SELECT * FROM ".DB::table('forum_attachment_'.$attach_forum_tableid)." WHERE remote =0 AND syndate>=0 AND syndate<10 $sql ORDER BY aid DESC LIMIT 30");
	foreach($query as $value){
		if(!$BIGfile){
			$attach_forum[$value['aid']] = $value;
			$remoteaids[$value['aid']] = $value['aid'];
			if($value['filesize'] > $BIGfile_size *1024 *1024){
				$BIGfile = $value['aid'];
			}
		}
	}
	
	unset($query,$value,$sql);
	if($attach_forum){
		DB::update('forum_attachment_'.$attach_forum_tableid,array('syndate'=> -1),"aid IN(".dimplode($remoteaids).")");
	}
	$homepic_ids = $aids = array();
	$OSS = & discuz_ftp_oss::instance();
	foreach($attach_forum as $key => $value){
		$value['ext'] = fileext($value['filename']);
		if(Cron_attach_if_ftp($value['filesize'],$value['ext'])) {
			$thumb_file = $value['thumb'] ? 'forum/'.getimgthumbname($value['attachment']) : '';
			
			if(ftpcmd('upload', 'forum/'.$value['attachment'],$value['filename']) && (!$thumb_file || ($thumb_file && file_exists($_G['setting']['attachdir'].$thumb_file) && ftpcmd('upload', $thumb_file)))) {
				
				if($_G['setting']['ftp']['on']=='1' || ($_G['setting']['ftp']['on']=='2' && $_G['setting']['ftp']['ftp_oss_delete'])){
					dunlink($value);
				}
				$ftpsuccess_aids[$value['aid']] = $value['aid'];
				unset($remoteaids[$value['aid']]);
				if($value['picid']) {
					$homepic_ids[] = $value['picid'];
				}
				//将上传成功后的附件变更标识
				DB::update('forum_attachment_'.$attach_forum_tableid,array('remote' => 1 ,'syndate'=>TIMESTAMP),"aid='".$value['aid']."'");
				//将上传成功后附件关联的主题封面变更标识
				DB::update('forum_threadimage',array('remote' => 1),"attachment='".daddslashes($value['attachment'])."'");
			}elseif(!file_exists($_G['setting']['attachdir'].'forum/'.$value['attachment']) && $OSS->ftp_size('forum/'.$value['attachment'])){//如果OSS上有附件 则更新为远程附件
				DB::update('forum_attachment_'.$attach_forum_tableid,array('remote' => 1 ,'syndate'=> TIMESTAMP),"aid='".$value['aid']."'");
			}elseif($value['syndate'] >= 0 && $value['syndate'] < 10){
				//将附件标记为失败
				DB::update('forum_attachment_'.$attach_forum_tableid,array('remote' => 0 ,'syndate'=> ($value['syndate'] +1)),"aid='".$value['aid']."'");
			}elseif($value['syndate'] == -1){
				//将锁定附件变更为正常
				DB::update('forum_attachment_'.$attach_forum_tableid,array('remote' => 0 ,'syndate'=> 0),"aid='".$value['aid']."'");
			}
		}elseif($value['syndate'] == -1){
			//将锁定附件变更为正常
			DB::update('forum_attachment_'.$attach_forum_tableid,array('remote' => 0 ,'syndate'=> 0),"aid='".$value['aid']."'");
		}
	}
	
	//将上传成功后关联相册的附件变更标识
	if($homepic_ids) {
		C::t('home_pic')->update($homepic_ids, array('remote' => 3));
	}
}

?>